/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
        ListNode* cur1 = pHead1;
		int cnt1 = 0, cnt2 = 0;
		while(cur1 != nullptr)
		{
			cnt1++;
			cur1 = cur1->next;
		}
		ListNode* cur2 = pHead2;
		while(cur2 != nullptr)
		{
			cnt2++;
			cur2 = cur2->next;
		}
		// cout << pHead1->val << " " << pHead2->val << endl;
		if(cnt1 < cnt2)
			swap(pHead1, pHead2);
		cur1 = pHead1, cur2 = pHead2;
		// cout << pHead1->val << " " << pHead2->val << endl;
		int diff = abs(cnt1 - cnt2);
		while(diff--)
		{
			cur1 = cur1->next;
		}
		while(cur1 != nullptr && cur2 != nullptr)
		{
			// cout <<cur1->val  << " " <<  cur2->val << endl;
			if(cur1->val == cur2->val)
				return cur1;
			cur1 = cur1->next;
			cur2 = cur2->next;
		}
		return nullptr;
    }
};